﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;

namespace DISDisplay
{
    class Functionality
    {
        public static bool isConference = true;
        public static string ConferrenceId = "-1";

        public static void renewLabel(Label lb1,Timer tm, Clock clk)
        {
            if (isConference)
            {
                lb1.Visible = true;
                clk.Visible = false;
                tm.Enabled = false ;
               int PersonsNumber=getNumber();
               lb1.Text = "Dalyvauja: " + PersonsNumber.ToString();
               if (PersonsNumber >= Configuration.QuarumLimit)
               {
                   lb1.ForeColor = Configuration.TextQColor;
               }
               else
               {
                   lb1.ForeColor = Configuration.TextNQColor;
               }
            }
            else
            {
                lb1.Visible = false;
                clk.Visible = true;
                tm.Enabled = true;
            }
        }
        public static int getNumber()
        {
            int rez = 0;
            DataBase db = new DataBase(Configuration.dbServer, Configuration.db_user, Configuration.db_password, Configuration.db);
            string sqltemp = "select distinct delegate_id from LogDelegate where conference_id=" + ConferrenceId + " and loginTime>(select startTime from LogConference where conference_id=" + ConferrenceId + " and stoptime is null) and logoutTime is null and delegate_id in (select id from Delegate left join UserTypeText on Delegate.usertype_id=UserTypeText.usertype_id where UserTypeText.name like '%Tarybos narys%' or UserTypeText.name like '%Pirmininkaujantis%' or UserTypeText.name like '%Mero pavaduotojas%' )";
            DataTable tb = db.getTable(sqltemp);
            if (tb.Rows.Count > 0)
            {
                if (tb.Columns.Contains("delegate_id"))
                {
                    rez = tb.Rows.Count;
                }
            }
            
            return rez;
        }
        public static bool getMeeting()
        {
            bool rez = false;
            String timenow = DateTime.Now.ToString();
            DataBase db = new DataBase(Configuration.dbServer, Configuration.db_user, Configuration.db_password, Configuration.db);
            //String sql = "select * from logConference where (starttime<='" + timenow + "' and stoptime>='" + timenow + "') or (starttime<='" + timenow + "' and stoptime is NULL)";
            String sql = "select * from Conference where active=1";
            DataTable tb = db.getTable(sql);
            if (tb.Rows.Count > 0)
            {
                if (tb.Columns.Contains("id"))
                {
                    ConferrenceId = tb.Rows[0]["id"].ToString();
                }
                rez = true;
            }
            else
            {
                ConferrenceId = "-1";
            }
            //rez = false; //test
            return rez;
        }

       
    }
}
